/**
 * Copyright 2024 NoEvaa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "testlib.h"
#include "aymmap/utility/endian.hpp"

using namespace aymmap;

TEST_CASE("byteswap") {
    CHECK(detail::byteswap(int8_t(0x12)) == int8_t(0x12));
    CHECK(detail::byteswap(int16_t(0x1234)) == int16_t(0x3412));
    CHECK(detail::byteswap(int32_t(0x12345678)) == int32_t(0x78563412));
    CHECK(detail::byteswap(int64_t(0x1234567891827364)) == int64_t(0x6473829178563412));

    CHECK(detail::byteswap(bool(1)) == true);
    CHECK(detail::byteswap(bool(0)) == false);

    CHECK(detail::byteswap(testlib::inplaceCast<float>(int32_t(0x01345602))) == 
          testlib::inplaceCast<float>(int32_t(0x02563401)));
    CHECK(detail::byteswap(testlib::inplaceCast<double>(int64_t(0x0134567893847502))) == 
          testlib::inplaceCast<double>(int64_t(0x0275849378563401)));
}

TEST_CASE("autoFitEndian") {
    CHECK(autoFitEndian<Endian::native>(int32_t(0x12345678)) == int32_t(0x12345678));
    constexpr auto endi = Endian::native == Endian::little ? Endian::big : Endian::little;
    CHECK(autoFitEndian<endi>(int32_t(0x12345678)) == int32_t(0x78563412));
}

